package com.ruoyi.project.remake.controller;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.system.domain.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.remake.domain.TRemake;
import com.ruoyi.project.remake.service.ITRemakeService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 翻拍程序用户管理Controller
 *
 * @author 孙山伟
 * @date 2020-12-13
 */
@RestController
@RequestMapping("/remake/remake")
public class TRemakeController extends BaseController {
    @Autowired
    private ITRemakeService tRemakeService;

    /**
     * 查询翻拍程序用户管理列表
     */
    @PreAuthorize("@ss.hasPermi('remake:remake:list')")
    @GetMapping("/list")
    public TableDataInfo list(TRemake tRemake) {
        startPage();
        List<TRemake> list = tRemakeService.selectTRemakeList(tRemake);
        return getDataTable(list);
    }

    /**
     * 导出翻拍程序用户管理列表
     */
    @PreAuthorize("@ss.hasPermi('remake:remake:export')")
    @Log(title = "翻拍程序用户管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TRemake tRemake) {
        List<TRemake> list = tRemakeService.selectTRemakeList(tRemake);
        ExcelUtil<TRemake> util = new ExcelUtil<TRemake>(TRemake.class);
        return util.exportExcel(list, "remake");
    }

    /**
     * @author 孙山伟
     * @date 2023/3/27 21:12
     * @参数[tRemake]
     * @返回值 com.ruoyi.framework.web.domain.AjaxResult
     */
    @PreAuthorize("@ss.hasPermi('remake:remake:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tRemakeService.selectTRemakeById(id));
    }

    /**
     * 新增翻拍程序用户管理
     */
    @PreAuthorize("@ss.hasPermi('remake:remake:add')")
    @Log(title = "翻拍程序用户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TRemake tRemake) {
        TRemake tRemake1 = new TRemake();
        tRemake1.setLoginName(tRemake.getLoginName());
        List<TRemake> remakeList = tRemakeService.selectTRemakeList(tRemake1);
        if (remakeList.size() != 0) {
            return AjaxResult.error("用户名不允许重复");
        }
        tRemake.setCreateBy(SecurityUtils.getUsername());
        tRemake.setPassword(SecurityUtils.encryptPassword(tRemake.getPassword()));
        return toAjax(tRemakeService.insertTRemake(tRemake));
    }

    /**
     * 修改翻拍程序用户管理
     */
    @PreAuthorize("@ss.hasPermi('remake:remake:edit')")
    @Log(title = "翻拍程序用户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TRemake tRemake) {
        return toAjax(tRemakeService.updateTRemake(tRemake));
    }

    /**
     * 删除翻拍程序用户管理
     */
    @PreAuthorize("@ss.hasPermi('remake:remake:remove')")
    @Log(title = "翻拍程序用户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tRemakeService.deleteTRemakeByIds(ids));
    }

    /**
     * 重置密码
     */
    @PreAuthorize("@ss.hasPermi('remake:remake:resetPwd')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping("/resetPwd")
    public AjaxResult resetPwd(@RequestBody TRemake tRemake) {
        tRemakeService.checkUserAllowed(tRemake);
        TRemake tRemake1 = new TRemake();
        tRemake1.setId(tRemake.getId());
        System.out.println(SecurityUtils.encryptPassword(tRemake.getPassword()));
        tRemake1.setPassword(SecurityUtils.encryptPassword(tRemake.getPassword()));
        tRemake1.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(tRemakeService.resetPwd(tRemake1));
    }
}
